package web.jdbc.function;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.StringJoiner;

import web.clazz.Clazz;
import web.jdbc.DataOperator;

/**用于查找Entity
 * @author 乐渊
 * @version 1.0
 * @see DataOperator
 * */
public class Select<T> {
	public static PreparedStatement ps = null;
	private Connection conn = null;
	private T t;
	private Clazz clazz = null;
	
	public Select(Connection conn, T t, Clazz clazz) {
		this.conn = conn;
		this.t = t;
		this.clazz = clazz;
	}

	public ResultSet getRS() throws SQLException{
		StringJoiner sql = new StringJoiner("");
		sql.add("SELECT * FROM "+clazz.getName());
		new DataOperator<T>().collectData(t, clazz);
		
		if(DataOperator.keyColumn != null){
			sql.add(" WHERE "+DataOperator.keyColumn+"=?");
			ps = conn.prepareStatement(sql.toString());
			DataOperator.setData(ps, 1, DataOperator.keyValue);
			return ps.executeQuery();
		}
		return null;
	}
	
}
